package com.tcoiss.app.controller;

import com.tcoiss.app.domain.FenceDistrict;
import com.tcoiss.app.service.IFenceDistrictService;
import com.tcoiss.common.core.web.controller.BaseController;
import com.tcoiss.common.core.web.domain.AjaxResult;
import com.tcoiss.common.log.annotation.Log;
import com.tcoiss.common.log.enums.BusinessType;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/district")
public class FenceDistrictController extends BaseController {

    private final IFenceDistrictService iFenceDistrictService;


    @GetMapping("/queryDistrict")
    public AjaxResult queryPsFence(FenceDistrict fenceDistrict) {
        List<FenceDistrict> list = iFenceDistrictService.queryList(fenceDistrict);
        return AjaxResult.success(list);
    }

    /**
     * 根据相应的区域查询相应的区域信息
     */
    //@PreAuthorize(hasPermi = "${integration:District}:list")
    @GetMapping("/getDistrictByCity")
    public AjaxResult getDistrictByCity(FenceDistrict fence) {
        return  AjaxResult.success(iFenceDistrictService.getDistrictInfo(fence,"getDistrictInfo"));
    }

    /**
     * 删除围栏,
     */
    //@PreAuthorize(hasPermi = "${integration:District}:remove" )
    @Log(title = "删除围栏" , businessType = BusinessType.DELETE)
    @PostMapping("/delDistrict" )
    public AjaxResult remove(@RequestBody FenceDistrict district) {
        return toAjax(iFenceDistrictService.delDistrict(district) ? 1 : 0);
    }

    /**
     * 根据相应的区域查询本地的区域信息
     */
    //@PreAuthorize(hasPermi = "${integration:District}:list")
    @GetMapping("/getLocalDistrictByCity")
    public AjaxResult getLocalDistrictByCity(FenceDistrict fence) {
        return  AjaxResult.success(iFenceDistrictService.queryList(fence));
    }


    /**
     * 保存区级
     */
    //@PreAuthorize(hasPermi = "${integration:District}:add" )
    @Log(title = "区县围栏坐标" , businessType = BusinessType.INSERT)
    @PostMapping("/saveDistrict")
    public AjaxResult addFenceDistrict(@RequestBody List<FenceDistrict> districts) {
        return toAjax(iFenceDistrictService.saveDistricts(districts) ? 1 : 0);
    }

    @Log(title = "乡镇围栏坐标" , businessType = BusinessType.INSERT)
    @PostMapping("/saveQyCache")
    public AjaxResult addFenceTowns(@RequestBody FenceDistrict districts) {
        return iFenceDistrictService.saveOrUpDataQyCache(districts);
    }

    /** 获取角色列表 */
    @GetMapping("/getTowsPopByRole")
    public AjaxResult getTowsPopByRole() {
        return AjaxResult.success(iFenceDistrictService.getTowsPopByRole());
    }

    @Log(title = "重置区县围栏" , businessType = BusinessType.OTHER)
    @PostMapping("/resetDistrict")
    public AjaxResult resetDistrict(@RequestBody FenceDistrict districts) {
        return toAjax(iFenceDistrictService.resetDistrict(districts) ? 1 : 0);
    }

    @Log(title = "重置乡镇围栏" , businessType = BusinessType.OTHER)
    @PostMapping("/resetTownDistrict")
    public AjaxResult resetTownDistrict(@RequestBody FenceDistrict districts) {
        return toAjax(iFenceDistrictService.resetTownDistrict(districts) ? 1 : 0);
    }

    @Log(title = "复制乡镇围栏" , businessType = BusinessType.OTHER)
    @PostMapping("/copyTownOrDistrict")
    public AjaxResult copyTownOrDistrict(@RequestBody FenceDistrict districts) {
        return toAjax(iFenceDistrictService.copyTownOrDistrict(districts) ? 1 : 0);
    }

    /**
     * 检查是否已被复制
     */
    @PostMapping("/checkIsCopy")
    public AjaxResult checkIsCopy(@RequestBody FenceDistrict districts) {
        return toAjax(iFenceDistrictService.checkIsCopy(districts) ? 1 : 0);
    }

    /**
     * 检查区域名称
     */
    //@PreAuthorize(hasPermi = "${integration:District}:add" )
    @Log(title = "围栏坐标" , businessType = BusinessType.INSERT)
    @PostMapping("/checkDistrict")
    public AjaxResult checkDistrict(@RequestBody FenceDistrict fenceDistrict) {
        Map<String,Object> map = new HashMap<>();
        if(iFenceDistrictService.checkDistrict(fenceDistrict)){
            map.put("code",500);
            map.put("msg","区域名称重复");
            return AjaxResult.success(map);
        }
        map.put("code",200);
        return AjaxResult.success(map);
    }
}
